// packageJ/new_distribution/index.js
const app = getApp();
import Dialog from "../../dist/dialog/dialog";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,

    level_name: "",
    avatar_image: '',
    sum_commission: 0,
    nickname: "",
    sum_redpack: 0,
    list: [],
    level_id: 0,
    next_level_name: "",


    commission_list: [],
    red_packet_list: [],

    full_level: false,
    page: 1,
    total_page: 1,

    language: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF8924',
    });
    this.getFrontend();
    this.getCommission();
  },
  onChange(e) {
    let active = e.detail.index;
    this.setData({
      active
    });

    this.data.page = 1;
    if (active == 1) {
      this.getMemberList(1);
    } else {
      this.getCommission(1);
    }

  },
  getMemberList(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.redpack.memberList"),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        let list = data.list;
        if (page == 1) {
          this.setData({
            red_packet_list: list.data
          })
        } else {
          let _list = this.data.red_packet_list.concat(list.data);
          this.setData({
            red_packet_list: _list
          });
        }
        this.data.page = list.current_page;
        this.data.total_page = list.last_page;
      }
    })
  },
  getFrontend() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.index.index"),
      success: resdata => {
        let {
          data,
          result,
          msg,
          basic_info
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        let title = "";
        if (basic_info) {
          title = basic_info.plugin_setting.commission_performance.plugin_name;
        } else {
          title = wx.getStorageSync("yz_basic_info").plugin_setting?.commission_performance.plugin_name;
        }
        wx.setNavigationBarTitle({
          title
        })
        this.setData({
          avatar_image: data.member.avatar_image,
          nickname: data.member.nickname,
          level_name: data.level_name,
          sum_commission: data.sum_commission,
          sum_redpack: data.sum_redpack,
          list: data.next_level.list,
          level_id: data.level_id,
          next_level_name: data.next_level.level_name,
        })
        if (data.level_id == data.next_level.level_id) {
          this.setData({
            full_level: true
          })
        }
      }
    })
  },
  getCommission(page = 1) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.commission-performance.frontend.commission.getList"),
      data: {
        page
      },
      success: resdata => {
        let {
          data,
          result,
          msg
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        let list = data.list;
        if (page == 1) {
          this.setData({
            commission_list: list.data
          })
        } else {
          let _list = this.data.commission_list.concat(list.data);
          this.setData({
            commission_list: _list
          });
        }
        this.data.page = list.current_page;
        this.data.total_page = list.last_page;
      }
    })
  },

  goto() {
    if (this.data.level_id == 0) {
      Dialog.alert({
        title: '未满足领取等级',
        confirmButtonText: "知道了"
      }).then(() => {
        // on close
      });
      return;
    }

    wx.navigateTo({
      url: '/packageJ/new_distribution/red_packet/index',
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.total_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getCommission(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})